/**
 * ...
 * @author HTML5
 */
 
function ImageLoader()
{
	
	var _public = this;
	var m_currentId = "";
	var m_total = 0;
	var m_files = new Array();
	var m_data = new Object();
	_public.signal = new Signal();
	
	
	
	
	
	_public.addFile = function( p_file, p_id )
	{
		m_files.push( {url:p_file, id:p_id} );
	};
	
	
	_public.load = function()
	{
		m_total = m_files.length;
		loadNextFile();
	};
	
	
	function loadNextFile()
	{
		var l_img = new Image();
		var l_obj;
		
		if( m_files.length == 0 )
		{	
			_public.signal.dispatchEvent(ImageLoader.LOAD_COMPLETE);
		}
		else
		{
			l_img.onload = completeHandler;
			l_obj = m_files.shift();
			m_currentId = l_obj.id;
			l_img.src = l_obj.url;
		}
	}
	
	
	function completeHandler(event)
	{
		var l_img = event.target;
		m_data[m_currentId] = l_img;
		
		_public.signal.dispatchEvent(ImageLoader.LOAD_PROGRESS);
		
		loadNextFile();	
	};
	
	_public.getProgression = function()
	{
		return parseInt( ( m_total - m_files.length ) / m_total * 100 );
	};
	
	_public.clean = function()
	{
		m_files = new Array();
		m_data = new Object();
	}
	
	_public.getData = function()
	{
		return m_data;
	};
	
	_public.getDataById = function( p_id )
	{
		return m_data[p_id];
	};
	
	_public.destroy = function()
	{
		m_files = null;
		m_data = null;
		delete this;
	}
	
}

ImageLoader.signal = null;

ImageLoader.LOAD_COMPLETE = "loadComplete";
ImageLoader.LOAD_PROGRESS = "loadProgress";